<?php
namespace Badmins\Controller;
class WxController extends CkadminController {
    public function index(){
    	$list = F('wx_basc');
    	$this->assign('va',$list);
        $this->display();
    }
    public function menu(){
    	$db = M('wx_menu');
    	$list = $db->where("parent_id='0'")->order("sort desc")->select();
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			$list[$i]['sub_button'] = $db->where("parent_id='{$va['id']}'")->order("sort desc")->select(); 
    		}
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    public function menu_two() {
    	$res = M('WxMenu')->where("parent_id='0'")->field("id,name")->select();
    	$this->assign('res',$res);
    	$id = I('id');
    	if($id){
    		$list = M('WxMenu')->where("id='{$id}'")->order("id desc")->find();
    		$this->assign('va',$list);
    	}
        $this->display();
    }
    /**
     * 保存微信菜单
     */
    public function save_menu(){
    	$db = D('WxMenu');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$res = $db->save($create);
    		}else{
    			if($create['parent_id']){
    				$count = $db->where("parent_id='{$create['parent_id']}'")->count();
    				if($count>=4){
    					showmsg(300,'二级菜单最多只能有五个');exit;
    				}
    			}else{
    				$count = $db->where("parent_id = '0'")->count();
    				if($count>2){
    					showmsg(300,'一级菜单最多只能有三个');exit;
    				}
    			}
    			$log = "添加一个微信菜单，菜单名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 发布微信菜单
     */
    public function sub_wx_menu(){
    	$list = M('WxMenu')->where("parent_id ='0'")->select();
    	if(empty($list)){
    		showmsg(300,'你还没有创建任何的菜单');exit();
    	}
    	$res = F('wx_basc');
    	$msg = \Org\Wx\wx::createmenu($res['wx_appid'],$res['wx_appsecret']);
    	if($msg['errcode'] == 0){
    		showmsg(200,'发布成功');
    	}else{
    		showmsg(300,$msg['errmsg']);
    	}
    }
    /**
     * 添加、编辑公众号
     */
    public function add_wx(){
    	$list = F('wx_basc');
    	$this->assign('va',$list);
    	$this->display();
    }
    /**
     * 保存微信基本配置
     */
    public function save_wx_basc(){
    	$basc = F('wx_basc')?F('wx_basc'):array();
    	$list = I('post.');
    	$newlist = array_merge($basc, $list);
    	F('wx_basc',$newlist);
    	showmsg(200,'保存成功');
    }
    /**
     * 删除公众号
     */
    public function del_wx(){
    	$list = F('wx_basc',null);
    	showmsg(200,'删除成功');
    }
    /**
     * 删除微信菜单
     */
    public function del_wx_menu(){
    	$id = I('get.id/d');
    	$db = M('wx_menu');
    	$res = $db->where("id='{$id}'")->field("name,parent_id")->find();
    	$log = "删除了一个微信菜单，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	if(empty($res['parent_id'])){
    		$db->where("parent_id='{$id}'")->delete();
    	}
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 微信文本素材
     */
    public function text(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('wx_text')->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('wx_text','',1,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 添加、编辑文本素材
     */
    public function wx_text(){
    	$id = I('id');
    	if($id){
    		$list = M('wx_text')->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存文本素材
     */
    public function save_wx_text(){
    	$db = M('wx_text');
    	$create = $db->create();
    	if(empty($create['text'])){
    		showmsg(300,'请输入文本内容');exit();
    	}
    	if($create['id']){
    		$res = $db->save($create);
    	}else{
    		$log = "添加一个微信文本素材，素材内容为".$create['text'];
    		$this->add_log($log);
    		$res = $db->add($create);
    	}
    	if($res!=false){
    		showmsg(200,'保存成功');
    	}else{
    		showmsg(300,'你没有做任何的修改，无需保存编辑');
    	}
    }
    /**
     * 删除微信文本素材
     */
    public function del_wx_text(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('wx_text');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("text")->find();
    		$log = "删除了一个微信文本素材，名为".$re['text'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 微信图文素材
     */
    public function imgtxt(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('wx_imgtext')->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('wx_imgtext','',1,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 添加、编辑图文素材
     */
    public function wx_imgtext(){
    	$id = I('id');
    	if($id){
    		$list = M('wx_imgtext')->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存图文素材
     */
    public function save_wx_imgtext(){
    	$db = D('WxImgtext');
    	if($create = $db->create()){
    		if($create['id']){
    			$res = $db->save($create);
    		}else{
    			$wx = F('wx_basc');
    			$wx_id = \Org\Wx\wx::add_img(4, $create['small_img'], $wx['wx_appid'], $wx['wx_appsecret']);
    			if($wx_id['thumb_media_id']){
    				$create['thumb_media_id'] = $wx_id['thumb_media_id'];
    			}else{
    				unlink(".".$create['small_img']);
    				unlink(".",$create['big_img']);
    				showmsg(300,$wx_id['errmsg']);exit();
    			}
    			$log = "添加一个微信图文素材，素材内容为".$create['title'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	};
    }
    /**
     * 删除微信图文素材
     */
    public function del_wx_imgtext(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('WxImgtext');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("title")->find();
    		$log = "删除了一个微信图文素材，名为".$re['title'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 微信图片素材
     */
    public function image(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('wx_image')->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('wx_image','',1,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     *删除微信图片素材
     */
    public function del_wx_image(){
    	$p = I('get.p');
    	$res = M('wx_image')->where(array('photo'=>$p))->find();
    	if(empty($res)){
    		showmsg(300,'删除错误');exit();
    	}
    	M('wx_image')->where("id='{$res['id']}'")->delete();
    	unlink(".".$p);
    	showmsg(200,'删除成功');
    }
    public function ceshi(){
          
    }
    /**
     * 语音素材
     */
    public function voice(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('wx_voice')->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('wx_voice','',1,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 添加、编辑语音素材
     */
    public function wx_voice(){
    	$id = I('id');
    	if($id){
    		$list = M('wx_voice')->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存语音素材
     */
    public function save_wx_voice(){
    	$db = D('WxVoice');
    	if($create = $db->create()){
    		if($create['id']){
    			$res = $db->save($create);
    		}else{
    			$wx = F('wx_basc');
    			$wx_id = \Org\Wx\wx::add_img(2, $create['file'], $wx['wx_appid'], $wx['wx_appsecret']);
    			if($wx_id['media_id']){
    				$create['wx_id'] = $wx_id['media_id'];
    			}else{
    				unlink(".".$create['file']);
    				showmsg(300,$wx_id['errmsg']);exit();
    			}
    			$log = "添加一个微信语音素材，语音名称为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	};
    }
    
    /**
     * 删除微信语音素材
     */
    public function del_wx_voice(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('WxVoice');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name,file")->find();
    		unlink(".".$re['file']);
    		$log = "删除了一个微信语音素材，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑视频素材
     */
    public function wx_video(){
    	$id = I('id');
    	if($id){
    		$list = M('WxVideo')->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 上传视频素材
     */
    public function save_wx_video(){
    	$db = D('WxVideo');
    	if($create = $db->create()){
    		if($create['id']){
    			$res = $db->save($create);
    		}else{
    			$wx = F('wx_basc');
    		    $wx_id = \Org\Wx\wx::add_img(3, $create['file'], $wx['wx_appid'], $wx['wx_appsecret']);
    			if($wx_id['media_id']){
    				$create['wx_id'] = $wx_id['media_id'];
    			}else{
    				unlink(".".$create['file']);
    				showmsg(300,$wx_id['errmsg']);exit();
    			}
    			$log = "添加一个微信视频素材，视频名称为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	};
    }
    /**
     * 视频素材
     */
    public function video(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$where['name'] = array('like',"%{$name}%");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('wx_video')->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('wx_video','',$where,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 删除微信视频素材
     */
    public function del_wx_video(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('WxVideo');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name,file")->find();
    		unlink(".".$re['file']);
    		$log = "删除了一个微信视频素材，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑自动回复
     */
    public function newkey(){
    	$id = I('id');
    	if($id){
    		$list = M('WxAutoreply')->where("id='{$id}'")->find();
    		if(!empty($list)){
    			$list['status_name'] = wx_status_name($list['status'], $list['status_id']);
    		}
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 上传自动回复关键字
     */
    public function save_autoreply(){
    	$db = D('WxAutoreply');
    	if($create = $db->create()){
    		if($create['id']){
    			$res = $db->save($create);
    		}else{
    			$log = "添加一个自动回复，关键字为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	};
    }
    /**
     * 自动回复关键字
     */
    public function autoreply(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$status = I('status/d');
    	if($status){
    		$where['status'] = $status;
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$where['name'] = array('like',"%{$name}%");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('WxAutoreply')->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('WxAutoreply','',$where,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 删除自动回复关键字
     */
    public function del_wx_autoreply(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('WxAutoreply');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个关键字回复，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 欢迎语
     */
    public function wel(){
    	$list = M('wx_welcome_word')->order("id desc")->find();
    	if(!empty($list['status'])){
    		$list['status_name'] = wx_status_name($list['status'], $list['status_id']);
    	}
    	$this->assign('va',$list);
    	$this->display();
    }
    /**
     * 保存欢迎语
     */
    public function save_wel(){
    	$db = M('wx_welcome_word');
    	$status = I('status');
    	if(empty($status)){
    		showmsg(300,'请选择欢迎语的类型');exit();
    	}
    	$status_id = D('WxAutoreply')->ckstatus();
    	if($status_id){
    		$re = $db->find();
    		$data['status'] = $status;
    		$data['status_id'] = $status_id;
    		if(empty($re)){
    			$db->add($data);
    		}else{
    			$data['id'] = $re['id'];
    			$db->save($data);
    		}
    		showmsg(200,'保存成功');
    	}else{
    		showmsg(300,'保存失败');
    	}
    }
    /**
     * 群发消息
     */
    public function mass(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$status = I('status/d');
    	if($status){
    		$where['status'] = $status;
    	}
        $start_time = I('start_time');
    	if($start_time){
    		$where['add_time'] = array('elt',$start_time);
    	}
    	$end_time = I('end_time');
    	if($end_time){
    		$where['add_time'] = array('egt',$end_time." 23:59:59");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('WxMass')->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('WxMass','',$where,"*","id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			if($va['user_id']){
    				$list[$i]['username'] = M('user_group')->where("id='{$va['user_id']}'")->getField('name');
    			}else{
    				$list[$i]['username'] = "所有会员";
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 添加群发消息
     */
    public function newmas(){
    	$group = M('user_group')->order("id desc")->field("id,name,discount")->select();
    	$this->assign('group',$group);
    	$this->display();
    }
    /**
     * 保存微信群发消息
     */
    public function save_newmass(){
    	$db = D('WxMass');
    	if($create = $db->create()){
    		$wx = F('wx_basc');
    		if($create['status'] == 2){
    			//图文
    			$data['id'] = array('in',$create['status_id']);
    			$re = M('wx_imgtext')->where($data)->field("author,title,url as content_source_url,content,intro as digest,thumb_media_id")->select();
    			foreach ($re as $i=>$va){
    				if($i == 0){
    					$re[$i]['show_cover_pic'] = 1;
    				}else{
    					$re[$i]['show_cover_pic'] = 0;
    				}
    			}
    			$b = \Org\Wx\wx::uploadnews($re, $wx['wx_appid'], $wx['wx_appsecret']);
    			if($b['media_id']){
    				$create['wx_id'] = $b['media_id'];
    			}else{
    				showmsg(300,$b['errmsg']);exit();
    			}
    		}elseif ($create['status'] == 1){
    			//文本
    			$create['wx_id'] = M('wx_text')->where("id='{$create['status_id']}'")->getField('text');
    		}elseif ($create['status'] == 3){
    			//语音
    			$create['wx_id'] = M('wx_voice')->where("id='{$create['status_id']}'")->getField('wx_id');
    		}elseif ($create['status'] == 4){
    			//视频
    			$media_id = M('wx_video')->where("id='{$create['status_id']}'")->field('wx_id,name,mark')->find();
    			$b = \Org\Wx\wx::uploadvideo($media_id['wx_id'],$media_id['name'],$media_id['mark'],$wx['wx_appid'], $wx['wx_appsecret']);
    		    if($b['media_id']){
    		    	$media_id['wx_id'] = $b['media_id'];
    		    	$create['wx_id'] = jsonencode($media_id);
    		    }else{
    		    	showmsg(300,$b['errmsg']);exit();
    		    }
    		}elseif ($create['status'] == 5){
    			//图片
    			$create['wx_id'] = M('wx_image')->where("id='{$create['status_id']}'")->getField('wx_id');
    		}
    		$create['send_status'] = 1;
    		$res = $db->add($create);
    		showmsg(200,'保存成功');exit();
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 发送群发消息
     */
    public function send_mass(){
    	$id = I('id');
    	$db = M('WxMass');
    	$list = $db->where("id='{$id}'")->find();
    	if(empty($list['wx_id'])){
    		showmsg(300,'此信息不存在');exit();
    	}
    	if(empty($list['user_id'])){
    	    $where['status'] = 0;
    	    $where['wx_code'] = array('neq','');
    	}else{
	    	$where['status'] = 0;
	    	$where['wx_code'] = array('neq','');
	    	$where['id'] = array('in',$list['user_id']);
    	} 
    	$wx = F('wx_basc');
    	$user_id = M('user')->where($where)->field("wx_code")->select();
    	if(!empty($user_id)){
	    	foreach ($user_id as $vv){
	    	    $user_id_array[] = $vv['wx_code'];
	    	}
	    	if($list['status'] == 4){
	    		$list['wx_id'] = json_decode($list['wx_id'],true);
	    	}
	    	$c = \Org\Wx\wx::send_nenmsg($list['status'],$user_id_array,$list['wx_id'], $wx['wx_appid'], $wx['wx_appsecret']);
	    	if(!empty($c['errcode'])){
	    	    showmsg(300,$c['errmsg']);exit();
	    	}else{
	    		$db->where("id='{$id}'")->setField('send_status',0);
	    		showmsg(200,'发送成功');
	    	}
    	}else{
    	    showmsg(300,'用户不存在');exit();
    	}
    }
    /**
     * 从数据中删除消息
     */
    public function del_mass(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('WxMass');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->find();
    		$log = "删除了一个群发消息";
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    
}